Page({
  data: {
    consultData: [],
    pageNo: 1,
    pageSize: 10,
    total: 0,
    hasMore: true,
    isLoading: false,
    isRefreshing: false
  },

  onLoad() {
    console.log('页面加载');
    this.loadData();
  },

  loadData(isLoadMore = false) {
    if (this.data.isLoading) return;
    
    this.setData({ isLoading: true });
    wx.showLoading({ title: '加载中...' });
  
    wx.request({
      url: `http://localhost:8080/api/my/notify/getNotifys`,
      data: {
        pageNo: this.data.pageNo,
        pageSize: this.data.pageSize
      },
      success: (res) => {
        console.log('接收到的数据:', res.data);
        if (res.data.code === 200) {  // 使用200作为成功码
          const newData = res.data.data.list;
          const total = res.data.data.total;

          this.setData({
            consultData: isLoadMore ? [...this.data.consultData, ...newData] : newData,
            total: total,
            hasMore: this.data.consultData.length + newData.length < total
          }, () => {
            console.log('更新后的consultData:', this.data.consultData);
          });
        } else {
          wx.showToast({ title: res.data.message || '加载失败', icon: 'none' });
        }
      },
      fail: (err) => {
        console.error('请求失败', err);
        wx.showToast({ title: '网络错误', icon: 'none' });
      },
      complete: () => {
        this.setData({ isLoading: false });
        wx.hideLoading();
        if (this.data.isRefreshing) {
          wx.stopPullDownRefresh();
          this.setData({ isRefreshing: false });
        }
      }
    });
  },

  loadMore() {
    if (this.data.hasMore && !this.data.isLoading) {
      this.setData({ pageNo: this.data.pageNo + 1 }, () => {
        this.loadData(true);
      });
    }
  },

  onRefresh() {
    this.setData({ pageNo: 1, isRefreshing: true }, () => {
      this.loadData();
    });
  },

  onReachBottom() {
    this.loadMore();
  },

  onPullDownRefresh() {
    this.onRefresh();
  }
});